package org.tea.inventory.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.dao.sys.SysDeptMapper;
import org.tea.inventory.dao.tea.*;
import org.tea.inventory.db.service.AlumniRecordStorageService;
import org.tea.inventory.domain.sys.SysDept;
import org.tea.inventory.domain.sys.UserInfo;
import org.tea.inventory.domain.tea.*;
import org.tea.inventory.model.vo.SupplierInfoVo;
import org.tea.inventory.service.ISupplierInfoService;
import org.tea.inventory.utils.TeaUtils;
import org.tea.inventory.utils.UserUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.util.RandomUtil.getRandom;

/**
 * 供应商信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
@Transactional
@Slf4j
public class SupplierInfoServiceImpl implements ISupplierInfoService
{
    @Autowired
    private AlumniRecordStorageService storageService;

    @Autowired
    private SupplierMessageMapper supplierMessageMapper;

    @Autowired
    private SupplierQualifiedMapper supplierQualifiedMapper;

    @Autowired
    private SupplierFileMapper supplierFileMapper;

    @Autowired
    private SupplierTypeMapper supplierTypeMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询供应商信息列表
     *
     * @param supplierInfoVo 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<SupplierInfoVo> selectSupplierInfoList(SupplierInfoVo supplierInfoVo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //查询所有供应商信息
        supplierInfoVo.setDelFlag("0");
        List<SupplierInfoVo> result = supplierMessageMapper.selectSupplierMessageList(supplierInfoVo);
        result.stream().forEach(bean -> {
            bean.setAddTimeStr(format.format(bean.getAddTime()));

            SupplierType supplierType = supplierTypeMapper.selectSupplierTypeByTypeId(bean.getTypeId());
            bean.setTypeName(supplierType.getTypeName());
            /*SysDept sysDept = deptMapper.selectDeptById(Long.valueOf(bean.getResponsibleDept()));
            bean.setDeptName(sysDept.getDeptName());*/
            bean.setIsEnableStr("0".equals(bean.getIsEnable()) ? "启用" : "停用");

            //查询资质相关数据
            SupplierQualified supplierQualified = new SupplierQualified();
            supplierQualified.setSupplierId(bean.getSupplierId());
            SupplierQualified qualified = supplierQualifiedMapper.selectSupplierQualifiedSelective(supplierQualified);
            if (qualified != null && qualified.getQualifiedFile() != null) qualified.setFiles(storageService.selectByPrimaryKeys(qualified.getQualifiedFile().split(",")));
            //给vo对象赋值
            if (qualified != null && qualified.getQualifiedFile() != null) {
                bean.setQualifiedId(qualified.getId());
                bean.setQualifiedName(qualified.getQualifiedName());
                bean.setQualifiedStartTime(qualified.getStartTime());
                bean.setQualifiedEndTime(qualified.getEndTime());
                bean.setQualifiedFile(qualified.getQualifiedFile());
                bean.setQualifiedFiles(storageService.selectByPrimaryKeys(qualified.getQualifiedFile().split(",")));
            }


            //查询营业执照相关数据
            SupplierQualified supplierBusiness = new SupplierQualified();
            supplierBusiness.setSupplierNum(bean.getSupplierNum());
            SupplierQualified business = supplierQualifiedMapper.selectSupplierQualifiedSelective(supplierBusiness);
            //给vo对象赋值
            if (business != null && business.getQualifiedFile() != null) {
                bean.setBusinessId(business.getId());
                bean.setBusinessName(business.getQualifiedName());
                bean.setBusinessStartTime(business.getStartTime());
                bean.setBusinessEndTime(business.getEndTime());
                bean.setBusinessFile(business.getQualifiedFile());
                bean.setBusinessFiles(storageService.selectByPrimaryKeys(business.getQualifiedFile().split(",")));
            }

            //查询财务报表/审计报告
            //财务报表
            SupplierFile supplierFileFinance = new SupplierFile();
            supplierFileFinance.setQualifiedId(bean.getSupplierNum());
            SupplierFile finance = supplierFileMapper.selectSupplierQualifiedSelective(supplierFileFinance);
            if (finance != null && finance.getFileId() != null) {
                finance.setFiles(storageService.selectByPrimaryKeys(finance.getFileId().split(",")));
                bean.setFinanceFileId(finance.getFileId());
                bean.setFinanceFiles(finance.getFiles());
            }

            //审计报告
            SupplierFile supplierFileAudit = new SupplierFile();
            supplierFileAudit.setQualifiedId(bean.getSupplierId());
            SupplierFile audit = supplierFileMapper.selectSupplierQualifiedSelective(supplierFileAudit);
            if (audit != null && audit.getFileId() != null) {
                audit.setFiles(storageService.selectByPrimaryKeys(audit.getFileId().split(",")));
                bean.setAuditFileId(audit.getFileId());
                bean.setAuditFiles(audit.getFiles());
            }
        });
        return result;
    }


    /**
     * 查询供应商类型
     *
     * @return 供应商类型集合
     */
    @Override
    public List<SupplierType> selectSupplierTypeList(SupplierInfoVo supplierInfoVo) {
        SupplierType supplierType = new SupplierType();
        supplierType.setDelFlag("0");
        supplierType.setTypeGroupId("CATEGORY");
        return supplierTypeMapper.selectSupplierTypeList(supplierType);
    }

    @Override
    public int insertSupplierInfo(SupplierInfoVo supplierInfoVo) {
        Date date = new Date();
        String supplierId = IdUtil.fastUUID();
        String supplierNum = TeaUtils.getNumber();

        //维护供应商信息对象
        supplierInfoVo.setSupplierId(supplierId);
        supplierInfoVo.setSupplierNum(supplierNum);
        supplierInfoVo.setAddUser(UserUtil.getUser().getLoginId());
        supplierInfoVo.setCompanyId(UserUtil.getUser().getCompanyId());
        supplierInfoVo.setAddTime(date);
        int result = supplierMessageMapper.insertSupplierMessage(supplierInfoVo);

        //维护资质信息对象
        SupplierQualified supplierQualified = new SupplierQualified();
        supplierQualified.setQualifiedId(IdUtil.fastUUID());
        supplierQualified.setSupplierId(supplierId);
        supplierQualified.setAddTime(date);
        supplierQualified.setQualifiedName(supplierInfoVo.getQualifiedName());
        supplierQualified.setQualifiedFile(supplierInfoVo.getQualifiedFile());
        supplierQualified.setStartTime(supplierInfoVo.getQualifiedStartTime());
        supplierQualified.setEndTime(supplierInfoVo.getQualifiedEndTime());
        supplierQualifiedMapper.insertSupplierQualified(supplierQualified);

        //维护营业执照信息对象
        SupplierQualified supplierBusiness = new SupplierQualified();
        supplierBusiness.setQualifiedId(IdUtil.fastUUID());
        supplierBusiness.setSupplierNum(supplierNum);
        supplierBusiness.setAddTime(date);
        supplierBusiness.setQualifiedName(supplierInfoVo.getBusinessName());
        supplierBusiness.setQualifiedFile(supplierInfoVo.getBusinessFile());
        supplierBusiness.setStartTime(supplierInfoVo.getBusinessStartTime());
        supplierBusiness.setEndTime(supplierInfoVo.getBusinessEndTime());
        supplierQualifiedMapper.insertSupplierQualified(supplierBusiness);

        //维护财务报表
        if (StringUtils.isNotBlank(supplierInfoVo.getFinanceFileId())) {
            SupplierFile finance = new SupplierFile();
            finance.setFileId(supplierInfoVo.getFinanceFileId());
            finance.setUploadName("财务报表");
            finance.setUploadType("财务报表");
            finance.setUploadPath("财务报表");
            finance.setQualifiedId(supplierId);
            finance.setDelFlag("0");
            finance.setAddTime(date);
            supplierFileMapper.insertSupplierFile(finance);
        }

        //维护审计报告
        if (StringUtils.isNotBlank(supplierInfoVo.getAuditFileId())) {
            SupplierFile audit = new SupplierFile();
            audit.setFileId(supplierInfoVo.getAuditFileId());
            audit.setQualifiedId(supplierNum);
            audit.setUploadName("维护审计");
            audit.setUploadType("维护审计");
            audit.setUploadPath("维护审计");
            audit.setAddTime(date);
            audit.setDelFlag("0");
            supplierFileMapper.insertSupplierFile(audit);
        }

        return result;
    }

    @Override
    public int updateSupplierInfo(SupplierInfoVo supplierInfoVo) {
        Date date = new Date();

        //维护供应商信息对象
        supplierInfoVo.setModifyTime(date);
        int result = supplierMessageMapper.updateSupplierMessage(supplierInfoVo);

        //维护资质信息对象
        SupplierQualified supplierQualified = new SupplierQualified();
        supplierQualified.setId(supplierInfoVo.getQualifiedId());
        supplierQualified.setQualifiedName(supplierInfoVo.getQualifiedName());
        supplierQualified.setQualifiedFile(supplierInfoVo.getQualifiedFile());
        supplierQualified.setStartTime(supplierInfoVo.getQualifiedStartTime());
        supplierQualified.setEndTime(supplierInfoVo.getQualifiedEndTime());
        supplierQualified.setModifyTime(date);
        supplierQualifiedMapper.updateSupplierQualified(supplierQualified);

        //维护营业执照信息对象
        SupplierQualified supplierBusiness = new SupplierQualified();
        supplierBusiness.setId(supplierInfoVo.getBusinessId());
        supplierBusiness.setQualifiedName(supplierInfoVo.getBusinessName());
        supplierBusiness.setQualifiedFile(supplierInfoVo.getBusinessFile());
        supplierBusiness.setStartTime(supplierInfoVo.getBusinessStartTime());
        supplierBusiness.setEndTime(supplierInfoVo.getBusinessEndTime());
        supplierBusiness.setModifyTime(date);
        supplierQualifiedMapper.updateSupplierQualified(supplierBusiness);

        //财务报表
        SupplierFile supplierFileFinance = new SupplierFile();
        supplierFileFinance.setQualifiedId(supplierInfoVo.getSupplierNum());
        SupplierFile finance = supplierFileMapper.selectSupplierQualifiedSelective(supplierFileFinance);
        finance.setFileId(supplierInfoVo.getFinanceFileId());
        supplierFileMapper.updateSupplierFile(finance);

        //审计报告
        SupplierFile supplierFileAudit = new SupplierFile();
        supplierFileAudit.setQualifiedId(supplierInfoVo.getSupplierId());
        SupplierFile audit = supplierFileMapper.selectSupplierQualifiedSelective(supplierFileAudit);
        audit.setFileId(supplierInfoVo.getAuditFileId());
        supplierFileMapper.updateSupplierFile(audit);

        return result;
    }

    @Override
    public int deleteSupplierInfoById(Long id) {
        SupplierInfoVo supplierInfoVo = new SupplierInfoVo();
        supplierInfoVo.setId(id);
        supplierInfoVo.setDelFlag("1");
        return supplierMessageMapper.updateSupplierMessage(supplierInfoVo);
    }

    /**
     * 更新供应商状态
     *
     * @param id 供应商信息主键
     * @param isEnable 供应商更新前状态
     * @return 结果
     */
    @Override
    public int updateStatus(Long id, Integer isEnable) {
        UserInfo user = UserUtil.getUser();
        SupplierInfoVo supplierInfoVo = new SupplierInfoVo();
        supplierInfoVo.setId(id);
        supplierInfoVo.setIsEnable(isEnable == 0 ? "1" : "0");
        supplierInfoVo.setModifyTime(new Date());
        supplierInfoVo.setModifyUser(user.getLoginId());
        return supplierMessageMapper.updateSupplierMessage(supplierInfoVo);
    }


}
